﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web.Client.Services.Users;
using XCore.Authorization;
using SaaS.Model.Dto.Users;

namespace Web.Client.Controllers
{
    /// <summary>
    /// 会员中心
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ClientControllerBase
    {
        private readonly UserAppService _userAppService;
        private readonly AdminAppService _adminAppService;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="userAppService"></param>
        public UserController(UserAppService userAppService, AdminAppService adminAppService)
        {
            _userAppService = userAppService;
            _adminAppService = adminAppService;
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, Permission]
        public ActionResult Info()
        {
            UserModel user = _userAppService.GetUserInfo(Session.UserId);
            return Ok(new
            {
                user.UserName,
            });
        }
        /// <summary>
        /// 后台登录
        /// </summary>
        /// <returns></returns>
        [HttpPost, Permission]
        public ActionResult Login([FromBody]int tenantId)
        {
            return Ok(_adminAppService.Login(Session.UserId, tenantId));
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="phonenumber">手机号码</param>
        /// <param name="password">密码</param>
        /// <param name="configPassword">确认密码</param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public ActionResult ResetPwd(string phonenumber, string password, string configPassword)
        {
            if (string.IsNullOrWhiteSpace(configPassword)) throw new ClientException("密码不能为空");
            if (password != configPassword) throw new ClientException("密码不一致");
            //_userAppService.ResetPwd(phonenumber, password);
            return Ok("重置成功");
        }
        /// <summary>
        /// 找回密码
        /// </summary>
        /// <param name="phonenumber">手机号码</param>
        /// <param name="code">验证码</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<string> RetrievePwd(string phonenumber, string code, string password)
        {
            //短信验证
            if (string.IsNullOrWhiteSpace(code)) throw new ClientException("验证码不能为空");
            //_userAppService.ResetPwd(phonenumber, password);
            return Ok("重置成功");
        }
    }
}